package com.lzt.admin.common.security.service;

import com.lzt.admin.dto.user.UserDTO;
import com.lzt.admin.system.dao.UserDAO;
import com.lzt.admin.vo.UserDetailsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
    private final UserDAO userDAO;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDTO user = userDAO.getUserByName(username);
        Optional.ofNullable(user).orElseThrow(()->new BadCredentialsException("用户名不存在"));
        return new UserDetailsVO(user);
    }
}
